package com.test.demo.aop;

import com.test.demo.annotation.MyAnno;
import com.test.demo.utils.j2cache.CacheKeyBuilder;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AOPConfig  {

    //切入点
    @Pointcut("execution(public * com.test.demo.controller.*.*(..))")
    public void point(){

    }


    //前置通知

    /**
     *
     * @param joinPoint
     */
    @Before("point()")
    public void doBefore(JoinPoint joinPoint){

        Signature signature = joinPoint.getSignature();
        System.out.println(signature.getDeclaringType());
        System.out.println(signature.getDeclaringTypeName());
        System.out.println(signature.getModifiers());
    }

    //后置通知
    //这里的returning和接收对应 即被后置的那个方法的返回值
    @AfterReturning(value = "point()",returning = "ret")
    public void doAfterReturning(Object ret){

    }

    //后置最终通知
    @After("point()")
    public void doAfter(){

    }


    /**
     * 环绕通知
     * 环绕通知第一个参数必须是org.aspectj.lang.ProceedingJoinPoint类型
     * @param proceedingJoinPoint
     * @return
     * @throws Throwable
     */
//    @Around("point()")
    @Around("@annotation(com.test.demo.annotation.MyAnno)")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //TODO

        Object[] args = proceedingJoinPoint.getArgs();


        String key = CacheKeyBuilder.generate("ad","1.id",args);
        String simpleName = proceedingJoinPoint.getTarget().getClass().getSimpleName();
        System.out.println(simpleName);
        //签名对象
        Signature signature =
                proceedingJoinPoint.getSignature();

        //参数列表个数
        int modifiers = signature.getModifiers();


        //方法签名对象
        MethodSignature methodSignature= (MethodSignature)proceedingJoinPoint.getSignature();
        methodSignature.getMethod().getName();
        MyAnno annotation = methodSignature.getMethod().getAnnotation(MyAnno.class);

        methodSignature.getReturnType();


        return proceedingJoinPoint.proceed();

    }

}
